//
//  UIImage+ImageWithColor.m
//  YinXinBao
//
//  Created by Ruichao Wang on 13-1-24.
//
//

#import "UIImage+ImageWithColor.h"

@implementation UIImage (ImageWithColor)

+ (UIImage *) createImageWithColor: (UIColor *) color withBounds:(CGRect)bounds
{
    CGRect rect=bounds;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == NULL)
    {
        UIGraphicsEndImageContext();
        return nil;
    }
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

-(UIImage*)blendImageWithColor:(UIColor*)color
{
    if (self == nil)
        return nil;
    
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, self.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == NULL)
    {
        UIGraphicsEndImageContext();
        return nil;
    }
    
    [self drawInRect:rect];
    
    CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
    
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

@end
